let arr = JSON.parse(localStorage.getItem(`data`)) || []

let form = document.querySelector(`.info`)
let tbody = document.querySelector(`tbody`)

let names = form.querySelectorAll(`[name]`);

form.addEventListener(`submit`, function (e) {
  e.preventDefault();
  let stu = {};
  for (let i = 0; i < names.length; i++) {
    if (names[i].value == ``) {
      return alert(`李在赣神魔`)
    }
    stu[names[i].name] = names[i].value;
  }
  stu[`id`] = arr.length ? arr[arr.length - 1].id + 1 : 1;
  arr.push(stu);
  form.reset();
  xr()
})
function xr() {
  tbody.innerHTML = ``;
  const a = arr.map(function (man, i) {
    return `<tr>
    <td>${man.id}</td>
    <td>${man.uname}</td>
    <td>${man.age}</td>
    <td>${man.gender}</td>
    <td>${man.salary}</td>
    <td>${man.city}</td>
    <td>
    <a href="javascript:" data-id=${i} class="del">删除</a>
    </td>
    </tr>`
  })
  const sum = a.join("");
  tbody.innerHTML = sum;
  localStorage.setItem(`data`, `${JSON.stringify(arr)}`)
}
tbody.addEventListener(`click`, function (e) {
  if (e.target.tagName === `A`) {
    arr.splice(e.target.dataset.id, 1)
    localStorage.setItem(`data`, `${JSON.stringify(arr)}`)
    xr()
  }
})
xr()